package edu.maping.easy.painter.plugins;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import controlP5.ControlP5;

import processing.core.PApplet;
import processing.core.PImage;
import processing.core.PVector;
import codeanticode.glgraphics.GLGraphicsOffScreen;
import ddf.minim.analysis.FFT;
import deadpixel.keystone.CornerPinSurface;
import edu.maping.easy.ControlFacade;
import edu.maping.easy.fisica.EasyMappingFPoly;
import edu.maping.easy.gui.ButtonList;
import edu.maping.easy.gui.GUIEvent;
import edu.maping.easy.gui.GuiFactory;
import edu.maping.easy.model.Point;
import edu.maping.easy.model.PolygonThing;
import edu.maping.easy.model.Thing;
import edu.maping.easy.model.World;
import edu.maping.easy.painter.EventPainter;
import edu.maping.easy.painter.ThingPainterDelegate;
import edu.maping.easy.painter.UtilPainter;
import edu.maping.easy.painter.cornerpin.CornerPin;
import edu.maping.easy.painter.napplet.EMNAppletAdapter;
import edu.maping.easy.sequence.ActionConf;
import edu.maping.easy.sequence.ticker.TickerListener;
import fisica.FBlob;
import fisica.FBody;
import fisica.FBox;
import fisica.FPoly;
import fisica.FWorld;
import fisica.Fisica;
import fisica.FisicaNonStatic;

public class CopyOfFisicaRenderer extends EMNAppletAdapter {

	/**
	 * Buttons and bodies
	 * 
	 * by Ricard Marxer
	 * 
	 * This example shows how to create a blob.
	 */

	FWorld world;

	int circleCount = 20;
	float hole = 50;
	float topMargin = 50;
	float bottomMargin = 300;
	float sideMargin = 100;
	float xPos = 0;

	public void setup() {
		size(400, 400,P2D);
//		smooth();

//		FisicaNonStatic fisicaNonStatic = new FisicaNonStatic();
//		fisicaNonStatic.init(this);
		//Fisica.init(this);

		world = new FWorld(width,height);
		world.setGravity(0, -300);

		FPoly l = new FPoly();
		l.vertex(width / 2 - hole / 2, 0);
		l.vertex(0, 0);
		l.vertex(0, height);
		l.vertex(0 + sideMargin, height);
		l.vertex(0 + sideMargin, height - bottomMargin);
		l.vertex(width / 2 - hole / 2, topMargin);
		l.setStatic(true);
		l.setFill(0);
		l.setFriction(1);
		world.add(l);

		FPoly r = new FPoly();
		r.vertex(width / 2 + hole / 2, 0);
		r.vertex(width, 0);
		r.vertex(width, height);
		r.vertex(width - sideMargin, height);
		r.vertex(width - sideMargin, height - bottomMargin);
		r.vertex(width / 2 + hole / 2, topMargin);
		r.setStatic(true);
		r.setFill(0);
		r.setFriction(1);
		world.add(r);
	}

	public void draw() {
		background(255,255);

		if ((frameCount % 20) == 1) {
			FBlob b = new FBlob();
			float s = random(30, 40);
			float space = (width - sideMargin * 2 - s);
			xPos = (xPos + random(s, space / 2)) % space;
			b.setAsCircle(sideMargin + xPos + s / 2, height - random(100), s,
					20);
			b.setStroke(0);
			b.setStrokeWeight(2);
			b.setFill(255,122);
			world.add(b);
		}

		world.step();
		world.draw();
	}

	@Override
	protected void setParameters(String name, float value) {
	}

	@Override
	public String getName() {
		return "FisicaRenderer";
	}

	@Override
	public ThingPainterDelegate createNewInstance() {
		Object object = parentNAppletManager.createEmbeddedNApplet(this
				.getClass().getName(), 200, 200);
		CopyOfFisicaRenderer fireCube = (CopyOfFisicaRenderer) object;
		fireCube.utilPainter = utilPainter.clone();
		return fireCube;		
	}


}
